<?php
 // 前台会员模型
 Class MemberModel extends Model{

     /**
      * 获得单条数据
      * @param $condition 条件
      * @return mixed
      */
     public function getMemberByOne($condition,$field){
         return $this->where($condition)->field($field)->find();
     }

     /**
      * 获得多条数据
      * @param $condition 条件
      * @param $field 字段
      * @param $order 排序
      * @param $limit 数量
      * @return mixed
      */
     public function getMemberByMore($condition,$field,$order,$limit){
         return $this->where($condition)->field($field)->order($order)->limit($limit)->select();
     }

     /**
      * @param $data 数据
      * @return mixed|void
      */
     public function insert($data){
         if(empty($data)){
             return;
         }
         return $this->add($data);
     }

     /**
      * 更新数据
      */
     public function updata($condition,$data){
         return $this->where($condition)->save($data);
     }

     /**
      * 微信登录注册
      */
     public function wxLoginRegister($result){

         // 查询该微信号是否存在数据库中
         $where['openid'] = $result['openid'];
         $where['name'] = $result['nickname'];
         $is_has = $this->getMemberByOne($where);
         if($is_has){
               return array('status'=>200,'msg'=>"该微信账号已注册本网站",'data'=>$is_has['id']);
               exit();
         }
         // 插入到本地会员数据表中
         $insert['name'] = $result['nickname'];
         $insert['openid'] = $result['openid'];
         $insert['face'] = $result['headimgurl'];
         $insert['created_at'] = time();
         $res = $this->insert($insert);
         if($res){
             return array('status'=>200,'msg'=>"插入成功",'data'=>$res);
             exit();
         }
         return array('status'=>400,'msg'=>"插入失败");
     }
 }